java Android应用程序无法运行,持续崩溃
我正在创建一个可以查看多个列表的应用程序,希望能够搜索列表。我是一名java初学者,我确信我的代码中有一些错误,但我希望能够对其进行测试,看看是否可以创建应用程序。如果我发现这太难创建,我会放弃这个项目。我这样问是因为如果这是一个简单的解决办法,我很接近,那么我希望看到它通过。谢谢你的帮助
主要活动
package com.rocklandrecycles.rcsw.rcswwheredoirecycle;
import 安卓.content.Intent;
import 安卓.media.Image;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ImageButton;
import 安卓.widget.ListAdapter;
import 安卓.widget.ListView;
import 安卓.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends ActionBarActivity {
private ImageButton imagebutton1, imagebutton2, imagebutton3, imagebutton4, imagebutton5, imagebutton6;
private EditText SearchBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagebutton1 = (ImageButton)findViewById(R.id.metal_button);
imagebutton2 = (ImageButton) findViewById(R.id.imageButton2);
imagebutton3 = (ImageButton) findViewById(R.id.imageButton3);
imagebutton4 = (ImageButton) findViewById(R.id.imageButton4);
imagebutton5 = (ImageButton) findViewById(R.id.imageButton5);
imagebutton6 = (ImageButton) findViewById(R.id.imageButton6);
SearchBox = (EditText) findViewById(R.id.search_box);
String[] MetalList= {"metal", "metal cans", "aluminum foil", "copper", "steel", "scrap metal",
"hangers", "aluminum can", "knife", "exercise equipment"};
ListAdapter theAdapter = new ArrayAdapter<String>(this, 安卓.R.layout.simple_list_item_1,
MetalList);
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
theListView.setAdapter(theAdapter);
theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
ListView theListView = (ListView) findViewById(R.id.metal_list_view);
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String MetalPicked = "Please put in Green Bin";
Toast.makeText(MainActivity.this, MetalPicked, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onMetalButtonClick(View view) {
Intent getmetalscreenIntent= new Intent(this,
MetalScreen.class);
final int result = 1;
getmetalscreenIntent.putExtra("callingActivity", "Main Activity");
getmetalscreenIntent.putExtra("MetalActivity" , "MetalList");
startActivityForResult(getmetalscreenIntent, result);
}
}
显示
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name=".MainActivity"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity 安卓:name=".MetalScreen"
安卓:label="MetalScreen"
安卓:theme="@style/AppTheme"/>
</application>
金属屏
package com.rocklandrecycles.rcsw.rcswwheredoirecycle;
import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.ListView;
public class MetalScreen extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.metal_layout);
Intent activityThatCalled = getIntent();
String previousActivity = activityThatCalled.getExtras().getString("callingActivity");
String previousActivity2 = activityThatCalled.getExtras().getString("MetalActivity");
ListView callingActivityList = (ListView) findViewById(R.id.metal_list_view);
}
}
主要活动
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.rcswwheredoirecyclethisproject.MainActivity"
安卓:gravity="top"
安卓:background="@drawable/recyclesymbol">
<LinearLayout
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:layout_below="@+id/LinearLayout2">
<ImageButton
安卓:id="@+id/metal_button"
安卓:layout_width="50dp"
安卓:layout_height="50dp"
安卓:contentDescription="@string/content"
安卓:text="@string/ImageButton1text"
安卓:onClick="onMetalButtonClick"
安卓:background="@drawable/metal_texture"
安卓:maxHeight="10dp"
安卓:maxWidth="10dp"
安卓:cropToPadding="true"
安卓:clickable="true"/>
<ImageButton
安卓:id="@+id/imageButton2"
安卓:layout_width="50dp"
安卓:layout_height="50dp"
安卓:contentDescription="@string/content"
安卓:text="@string/ImageButton2text"
安卓:background="@drawable/biohazard"
安卓:clickable="true"/>
<ImageButton
安卓:id="@+id/imageButton3"
安卓:layout_width="50dp"
安卓:layout_height="50dp"
安卓:contentDescription="@string/content"
安卓:text="@string/ImageButton3text"
安卓:clickable="true"/>
<ImageButton
安卓:id="@+id/imageButton4"
安卓:layout_width="50dp"
安卓:layout_height="50dp"
安卓:contentDescription="@string/content"
安卓:text="@string/ImageButton4text"/>
<ImageButton
安卓:id="@+id/imageButton5"
安卓:layout_width="50dp"
安卓:layout_height="50dp"
安卓:contentDescription="@string/content"
安卓:text="@string/ImageButton5text"
安卓:clickable="true"/>
<ImageButton
安卓:id="@+id/imageButton6"
安卓:layout_width="50dp"
安卓:layout_height="50dp"
安卓:contentDescription="@string/content"
安卓:text="@string/ImageButton6text"
安卓:clickable="true"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal"
安卓:weightSum="5"
安卓:id="@+id/LinearLayout2"
安卓:layout_below="@+id/LinearLayout3">
<EditText
安卓:layout_width="200dp"
安卓:layout_height="150dp"
安卓:inputType="text"
安卓:hint="@string/EditText"
安卓:id="@+id/search_box"/>
</LinearLayout>
<LinearLayout
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:id="@+id/LinearLayout3"
安卓:layout_centerHorizontal="true">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="RCSW Recycle"
安卓:textSize="25sp"/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Where Do I Recycle This?"
安卓:textSize="25sp"/>
</LinearLayout>
<requestFocus />
</RelativeLayout>
金属布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent" 安卓:layout_height="match_parent">
<ListView
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
安卓:layout_gravity="left"
安卓:id="@+id/metal_list_view"
安卓:padding="10dp">
</ListView>
</LinearLayout>
刺痛
<resources>
<string name="app_name">RCSW Where Do I Recycle</string>
<string name="content">Image</string>
<string name="menu_settings">Settings</string>
<string name="ImageButton1text">Metal</string>
<string name="ImageButton2text">Hazard</string>
<string name="ImageButton3text">Paper</string>
<string name="ImageButton4text">Glass</string>
<string name="ImageButton5text">Plastic</string>
<string name="ImageButton6text">Other</string>
<string name="EditText">Search Box</string>
<string name="action_settings">RCSWRecycle</string>
</resources>
共 (0) 个答案